package com.nondo.rdp.finance.endpoint.wap.app;

import com.nondo.rdp.finance.domain.Article;
import com.nondo.rdp.finance.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller("apiWapArticle")
@RequestMapping("/wap/app/article")
public class ArticleEndpoint {

    @Autowired
    ArticleService articleService;

    /**
     * 获取文章详情
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/info/{type}")
    public ModelAndView getArticleByType(HttpServletRequest request, HttpServletResponse response, @PathVariable("type") String type) {
        ModelAndView mv = new ModelAndView("wap/app/article/info");

        Map map = new HashMap();

        Article article = this.articleService.findByType(type);
        if (article == null) {
            map.put("id", 0);
            map.put("title", "不存在的文章");
            map.put("body", "不存在的文章");
        } else {
            map.put("title", article.getTitle());
            map.put("body", article.getBody());
            map.put("id", article.getId());
        }

        mv.addObject("article", map);

        return mv;
    }
}